package com.iskny.framework.config;



import org.bee.tl.core.GroupTemplate;
import org.bee.tl.ext.jfinal.BeetlRenderFactory;

import com.iskny.framework.admin.interceptor.AdminDataInterceptor;
import com.iskny.framework.admin.interceptor.JsonRenderInterceptor;
import com.iskny.framework.model.Constant;
import com.iskny.framework.model.Data;
import com.iskny.framework.model.Menu;
import com.iskny.framework.model.Module;
import com.iskny.framework.model.Operation;
import com.iskny.framework.model.Relative;
import com.iskny.framework.model.Role;
import com.iskny.framework.model.Siteurl;
import com.iskny.framework.model.User;
import com.iskny.framework.model.VirtualStateEval;
import com.iskny.framework.web.index.IndexAction;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;

public class IsknyConfig extends JFinalConfig {

	private String json = java.lang.System.getenv("VCAP_SERVICES");
    private boolean isLocal = StringKit.isBlank(json);
	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("classes/config.txt");
        if (isLocal) {
            me.setDevMode(true);
        }
        me.setError404View("/common/404.html");
        me.setError500View("/common/500.html");
        me.setMainRenderFactory(new BeetlRenderFactory());
        GroupTemplate gt = BeetlRenderFactory.groupTemplate;
        gt.registerVirtualAttributeEval(new VirtualStateEval());//处理model状态
        gt.setStatementStart("@");
        gt.setStatementEnd(null);
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexAction.class);
		me.add(AdminRoute.me());
	}

	@Override
	public void configPlugin(Plugins me) {
		C3p0Plugin cp = new C3p0Plugin("jdbc:mysql://localhost/iskny_biz", "root", "tewq891129");
		me.add(cp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
//		arp.setShowSql(true);
		me.add(arp);
		arp.addMapping("t_user", User.class);
		arp.addMapping("t_role", Role.class);
		arp.addMapping("t_menu", Menu.class);
		arp.addMapping("t_siteurl", Siteurl.class);
		arp.addMapping("t_module", Module.class);
		arp.addMapping("t_operation", Operation.class);
		arp.addMapping("t_relative", Relative.class);
		arp.addMapping("t_data", Data.class);
		arp.addMapping("t_constant", Constant.class);
		
		
		// 缓存插件
        me.add(new EhCachePlugin());

	}

	@Override
	public void configInterceptor(Interceptors me) {
		//拦截配置
		me.add(new SessionInViewInterceptor());//view访问session
		me.add(new JsonRenderInterceptor());//自动json转换
		me.add(new AdminDataInterceptor());//admin基本数据设置
		
		//事务配置
//		me.add(new TxByRegex(".*save.*"));
//		me.add(new TxByRegex(".*delete.*"));
//		me.add(new TxByActionKeys("/cash/trans", "/other"));
//		me.add(new TxByActionMethods("save", "update"));
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new CommonDataHandler());//设置ctxPath
	}
	@Override
	/**
	 * Call back after JFinal start
	 */
	public void afterJFinalStart(){
		
	};
	@Override
	/**
	 * Call back before JFinal stop
	 */
	public void beforeJFinalStop(){};
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
     * 第一个参数填写的是“WEB-INF”文件夹的父文件夹名称
     * 第二个参数是设置访问的端口号
     * 第三个参数是设置该项目的访问根目录
     * 第四个参数是设置jetty每隔几秒钟扫描文件变化并重启应用
	 */
	public static void main(String[] args) throws Exception{
		JFinal.start("WebRoot", 80, "/iskny", 5);
	}

}
